package com.lovi.service;


import com.lovi.dao.MenuDao;
import com.lovi.domain.DiningTable;
import com.lovi.domain.Menu;
import com.lovi.utils.ScannerUtils;

import java.util.List;

public class MenuService {
    private MenuDao menuDao = new MenuDao();
    private DiningTableService diningTableService = new DiningTableService();
    private BillService billService = new BillService();

    public Menu getMenuById(int orderMenuId) {
        Menu menu = menuDao.getOneById(Menu.class, "select * from menu where id = ?", orderMenuId);
        return menu;
    }

    public List<Menu> list() {
        List<Menu> list = menuDao.list(Menu.class, "select * from menu", null);
        return list;
    }

    public void orderMenu(String empId) {
        System.out.println("=========点餐服务==========");
        System.out.print("请输入点餐的桌号(-1退出): ");
        Integer tableId =  ScannerUtils.readInt(10);
        if(tableId == -1) return;
        DiningTable table = diningTableService.getDiningTableById(tableId);
        if(table==null){
            System.out.println("该桌号不存在!!!");
            System.out.println("=========点餐失败==========");
            return;
        }
        //桌子没被使用的情况
        if("空".equals(table.getState())){
            //则预定餐桌
            if(diningTableService.orderDiningTable(empId)){
                table = diningTableService.getDiningTableById(tableId);
                System.out.println("=========预定成功==========");
            }else {
                System.out.println("=========预定失败==========");
            }
        }
        //桌子已经使用的情况
        if(!empId.equals(table.getOrderId())){
            System.out.println("该餐桌已被其他用户使用,请选择其他餐桌!");
            return;
        }
        //桌号存在，并且桌号与当前登录用户id相同，可以点餐
        System.out.println("请选择要菜品编号（-1退出）：");
        Integer menuId =  ScannerUtils.readInt(10);
        if(menuId == -1) return;
        Menu menu = this.getMenuById(menuId);

        if(menu==null){
            System.out.println("该菜品不存在!!!");
            System.out.println("=========点餐失败==========");
            return;
        }
        System.out.println("请输入点餐的菜品数量(-1退出): ");
        Integer nums =  ScannerUtils.readInt(10);
        if(nums == -1) return;
        double orderMoney =  menu.getPrice()*nums;
        //把点餐的信息保存到账单表(这里为了省事情，我们的账单id就是empId，这样我们查看账单的时候，就查找当前的empId未结账的账单)
        boolean b = billService.orderMenu(empId,menuId, nums, orderMoney,tableId);
        if(b) {
            System.out.println("=========点餐成功==========");
        }else {
            System.out.println("=========点餐失败==========");
        }

    }
}
